package com.jorado.etl.service;

import com.jorado.core.Result;
import com.jorado.core.utility.IOUtils;
import com.jorado.etl.model.TagInput;
import com.jorado.etl.model.TagResult;
import com.jorado.event.EventClient;
import com.jorado.json.JsonUtils;
import com.jorado.search.core.service.impl.ExportToFileImpl;
import com.jorado.search.core.service.impl.SolrExporter;
import com.jorado.search.core.util.SolrUtils;
import com.jorado.search.solr.client.SolrjOption;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.common.SolrDocument;
import org.springframework.context.annotation.Description;

import java.util.ArrayList;
import java.util.List;

@Description("Solr fasttext 标签服务")
public final class SolrFastTextTagService extends ExportToFileImpl<SolrDocument> {

    private int total;
    private FastTextTagService tagService;

    public SolrFastTextTagService(SolrjOption positionSolr, String distFile) {
        this(positionSolr, getSolrQuery(), distFile);

    }

    public SolrFastTextTagService(SolrjOption positionSolr, SolrQuery solrQuery, String distFile) {
        super(new SolrExporter(positionSolr, solrQuery), distFile, false);
        this.tagService = new FastTextTagService();
        this.setAfterFilter(this::after);
    }

    private static SolrQuery getSolrQuery() {
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery("*:*");
        solrQuery.addFilterQuery("JOB_SOURCE_TYPE:0");
        solrQuery.addFilterQuery("-JOB_TYPE_NAME:其他");
        //solrQuery.addFilterQuery("JOB_TYPE_NAME:其他");
        solrQuery.setFields("JOB_TITLE,JOB_TYPE_NAME,JOB_DESC,JOB_TYPE");
        solrQuery.setSort("DATE_CREATED", SolrQuery.ORDER.asc);
        return solrQuery;
    }

    @Override
    protected void export(List<SolrDocument> dataList) {

        total += dataList.size();

        String jobTitle;
        String jobType;
        String jobDescription;
        String jobTypeId;
        List<String> tagResults = new ArrayList<>();

        for (SolrDocument doc : dataList) {

            jobTypeId = SolrUtils.getDocValue(doc, "JOB_TYPE");
            jobType = SolrUtils.getDocValue(doc, "JOB_TYPE_NAME");
            jobTitle = SolrUtils.getDocValue(doc, "JOB_TITLE");
            jobDescription = SolrUtils.getDocValue(doc, "JOB_DESC");

            TagInput tagInput = new TagInput();
            tagInput.setJobTypeId(jobTypeId);
            tagInput.setJobType(jobType);
            tagInput.setJobTitle(jobTitle);
            tagInput.setJobDescription(jobDescription);

            TagResult tagResult = tagService.doTag(tagInput, 5);

            if (tagResult != null) {

                tagResults.add(JsonUtils.toJson(tagResult));
            }

        }

        try {
            IOUtils.appendLines(this.distFile, tagResults);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    protected Result after(Result result) {

        EventClient.getDefault().asyncSubmitLog(String.format("共打标签职位:[%d]个", total));

        //TODO:对文件去重
        return Result.OK;
    }
}
